package com.softeem.hbsm_video.controller;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.softeem.hbsm_video.entity.Result;
import com.softeem.hbsm_video.entity.User;
import com.softeem.hbsm_video.service.UserService;
import com.softeem.hbsm_video.util.JwtUtil;
import com.softeem.hbsm_video.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController { /*
    1、用户注册
    2、用户登录
    3、修改用户信息
    4、用户列表查询
    5、根据ID删除用户信息

     */
    @Autowired
    private UserService userService;

    //添加用户
    @GetMapping("/add")
    public Result add(User user){
        boolean exists =  userService.exists(Wrappers.query(User.class).eq("username", user.getUsername()));
        if(exists){
            return Result.error("用户名已存在");
        }
        //生成随机盐
        // 生成随机盐值，对密码进行加盐加密，然后保存用户信息
        String salt = RandomUtil.randomString(16); // 生成16位随机盐值
        String pwd=user.getPassword()+salt; // 将密码和盐值拼接
        pwd= SecureUtil.md5(pwd);
        user.setPassword(pwd);
        user.setSalt(salt);
        userService.save(user);
        return Result.success("用户添加成功");
    }

    @PostMapping("/login")
    public Result<String> login(String username,String password){
        // 1.
        User user = userService.getOne(Wrappers.query(User.class).eq("username",username));
        if(user==null){
            return Result.error("用户名不存在");
        }
        else{
            String salt = user.getSalt();
            String pwd = SecureUtil.md5(password + salt);
            if(pwd.equals(user.getPassword())){
                // 生成token
                Map<String, Object> claims=new HashMap<>();
                claims.put("id",user.getId());
                claims.put("name",user.getUsername());
                String token = JwtUtil.getToken(claims);
                return Result.success(token);
            }
            else{
                return Result.error("密码错误");
            }
        }
    }

    @PostMapping("/updateUser")
    public Result<String> updateUser(Long id, String newPassword) {
        User user = userService.getById(id);
        if (user == null) {
            return Result.error("用户不存在");
        }
        String salt = user.getSalt();
        String pwd = SecureUtil.md5(newPassword + salt);
        user.setPassword(pwd);
        userService.updateById(user);
        return Result.success("用户信息更新成功");
    }

    @PostMapping("/deleteUser")
    public Result<String> deleteUser(Long id) {
        boolean success = userService.removeById(id);
        if (success) {
            return Result.success("用户删除成功");
        } else {
            return Result.error("用户删除失败");
        }
    }

    @GetMapping("/infoUser") //获取当前登录管理员信息
    public Result<User> info() {
        Map<String, Object> map = ThreadLocalUtil.get();
        User user = userService.getById((Integer) map.get("id"));
        if (user == null) {
            return Result.error("获取失败");
        }
        return Result.success(user);
    }

}
